﻿Namespace Catalog
    Public Class Mix
        Inherits BaseObject
        Public Property AudioFormat As AudioFormat
        Public Property BitDepth As String 'Integer
        Public Property BitRate As Integer
        Public Property Description As String
        Public Property FileSize As Long
        Public Property DjProfile As DjProfile
        Public Property Images As List(Of Image)
        Public Property DynamicImages As List(Of Image)
        Public Property Artists As List(Of Artist)
        Public Property Genres As List(Of Genre)
        Public Property Badges As List(Of BaseObject)
        Public Property IsFree As Boolean
        Public Property IsPreviewOnly As Boolean
        Public Property IsVariableBitrate As Boolean
        Public Property Labels As List(Of Label)
        Public Property Length As TimeSpan
        Public Property Owner As String
        Public Property OriginalTrackId As Integer?
        Public Property Preview 'As IDictionary(Of String, IDictionary(Of String, String))
        Public Property Price As Price
        Public Property PublishDate As Date
        Public Property Published As Boolean
        Public Property RtmpStreamUrl As Uri
        Public Property SampleRate As Integer
        Public Property SampleUrl As Uri
        Public Property SKU As String
        Public Property Status As Boolean
        Public Property Views As Integer

        Public Sub New()
            Me.Artists = New List(Of Artist)
            Me.Badges = New List(Of BaseObject)
            Me.Images = New List(Of Image)
            Me.DynamicImages = New List(Of Image)
            Me.Genres = New List(Of Genre)
            Me.Labels = New List(Of Label)
        End Sub

        Public Shared Function FromBPObject(mx As BPOMix) As Mix
            If mx Is Nothing Then Return Nothing
            Dim newMix As New Mix()

            With newMix
                .ID = mx.id
                .Name = mx.name
                .Slug = mx.slug

                Select Case mx.audioFormat.ToLower
                    Case "mp3"
                        .AudioFormat = AudioFormat.Mp3
                    Case "m4a"
                        .AudioFormat = AudioFormat.M4a
                    Case "wav"
                        .AudioFormat = AudioFormat.Wav
                    Case "zip"
                        .AudioFormat = AudioFormat.Zip
                    Case "full download"
                        .AudioFormat = AudioFormat.FullDownload
                    Case "apple loops"
                        .AudioFormat = AudioFormat.AppleLoops
                    Case "rex"
                        .AudioFormat = AudioFormat.Rex
                    Case "synth presets"
                        .AudioFormat = AudioFormat.SynthPresets
                    Case "sampler patches"
                        .AudioFormat = AudioFormat.SamplerPatches
                    Case "reason refill"
                        .AudioFormat = AudioFormat.ReasonRefill
                    Case "elements"
                        .AudioFormat = AudioFormat.Elements
                    Case "s4"
                        .AudioFormat = AudioFormat.S4
                    Case "ableton live racks"
                        .AudioFormat = AudioFormat.AbletonLiveRacks
                    Case "aiff"
                        .AudioFormat = AudioFormat.Aiff
                    Case "mp4"
                        .AudioFormat = AudioFormat.Mp4
                End Select

                .BitDepth = mx.bitDepth
                .BitRate = mx.bitRate
                .Description = mx.description
                .FileSize = mx.fileSize
                If mx.djProfile IsNot Nothing Then
                    .DjProfile = New DjProfile(mx.djProfile.id, mx.djProfile.name, mx.djProfile.slug, mx.djProfile.personId)
                End If

                If mx.images IsNot Nothing Then
                    For Each img As KeyValuePair(Of String, BPOImage) In mx.images
                        .Images.Add(Image.FromBPObject(img))
                    Next
                End If

                If mx.dynamicImages IsNot Nothing Then
                    For Each dimg As KeyValuePair(Of String, BPOImage) In mx.dynamicImages
                        Dim newDImg As Image = Image.FromBPObject(dimg)
                        newDImg.IsDynamic = True
                        .Images.Add(newDImg)
                    Next
                End If

                If mx.artists IsNot Nothing Then
                    For Each art As BPOArtist In mx.artists
                        .Artists.Add(Artist.FromBPObject(art))
                    Next
                End If

                If mx.genres IsNot Nothing Then
                    For Each gen As BPOGenreBase In mx.genres
                        Dim newGen As Genre = New Genre() With {.ID = gen.id, .Name = gen.name, .Slug = gen.slug}

                        For Each subGen As BPOSubGenreBase In mx.subgenres
                            newGen.Subgenres.Add(New BaseObject(subGen.id, subGen.name, subGen.slug))
                        Next

                        .Genres.Add(newGen)
                    Next
                End If

                If mx.badges IsNot Nothing Then
                    For Each bad As BPOBase In mx.badges
                        .Badges.Add(New BaseObject(bad.id, bad.name, bad.slug))
                    Next
                End If

                .IsFree = mx.isFree
                .IsPreviewOnly = mx.isPreviewOnly
                .IsVariableBitrate = mx.isVariableBitrate

                If mx.labels IsNot Nothing Then
                    For Each lab As BPOLabel In mx.labels
                        .Labels.Add(Label.FromBPObject(lab))
                    Next
                End If

                TimeSpan.TryParseExact(mx.length, {"mm\:ss", "g"}, Nothing, .Length)
                .Owner = mx.mixOwner
                .OriginalTrackId = mx.originalTrackId
                .Preview = mx.preview
                .Price = Price.FromBPObject(mx.price)
                Date.TryParseExact(mx.publishDate, {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}, Nothing, Globalization.DateTimeStyles.None, .PublishDate)
                .Published = mx.published
                .RtmpStreamUrl = New Uri(mx.rtmpStreamUrl)
                .SampleRate = mx.sampleRate
                .SKU = mx.sku
                .Status = mx.status
                .Views = mx.views
            End With

            Return newMix
        End Function
    End Class
End Namespace